package com.task.core.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.task.common.enums.QuestionOperationTypeEnum;
import com.task.common.enums.QuestionPromptContentEnum;
import com.task.common.info.QuestionShowInfo;
import com.task.common.redis.RedisKeyConstants;
import com.task.core.domain.IpFraudPlatformInfo;
import com.task.core.service.IpFraudPlatformService;
import com.task.core.vo.resp.IpFraudIDetailVo;
import io.jsonwebtoken.lang.Collections;
import org.springframework.stereotype.Service;

import com.task.common.annotation.DataScope;
import com.task.common.core.redis.RedisCache;
import com.task.common.domain.info.KeyAndValue;
import com.task.common.enums.QuestionInfoTypeEnum;
import com.task.common.exception.ServiceException;
import com.task.common.info.IpFraudInfo;
import com.task.common.utils.SecurityUtils;
import com.task.common.utils.StringUtils;
import com.task.common.utils.uuid.UUID;
import com.task.core.dto.request.IpFraudRequestDTO;
import com.task.core.dto.request.QuestionInfoRequestDTO;
import com.task.core.mapper.IpFraudMapper;
import com.task.core.service.IpFraudService;
import com.task.core.service.QuestionInfoService;

/**
 * @author 迪迦.
 * @date 2024/11/4 17:27
 */
@Service("IpFraudService")
public class IpFraudServiceImpl implements IpFraudService {

  @Resource
  private IpFraudMapper ipFraudMapper;

  @Resource
  private RedisCache redisService;

  @Resource
  private QuestionInfoService questionInfoService;

  @Resource
  private IpFraudPlatformService ipFraudPlatformService;

  @Override
  @DataScope(deptAlias = "dept", userAlias = "su")
  public List<IpFraudInfo> getIpFraudByPage(IpFraudRequestDTO request) {
    //request.setOperationBy(SecurityUtils.checkAdmin());
    List<IpFraudInfo> ipFraudByPage = ipFraudMapper.getIpFraudByPage(request);
    for (IpFraudInfo info : ipFraudByPage) {
      List<QuestionShowInfo> questionShowInfoList = questionInfoService.getQuestionInfosByTypeAndCode(QuestionInfoTypeEnum.IP_FRAUD.name(), info.getCode());
      questionShowInfoList.stream().
              forEach(f ->{
                if( QuestionOperationTypeEnum.DELETE.getCode().equals(f.getOperationType()) &&
                        QuestionInfoTypeEnum.IP_FRAUD.name().equals(f.getQuestionType()) ){
                  f.setShowContent(info.getIpFraudName()+","+QuestionPromptContentEnum.IP_FRAUD_DELETE_PROMPT_CONTENT.getMsg());
                }else {
                  f.setShowContent(info.getIpFraudName()+","+f.getShowContent());
                }
              });
      info.setQuestionInfos(questionShowInfoList);
    }
    return ipFraudByPage;

  }

  @Override
  @DataScope(deptAlias = "dept", userAlias = "su")
  public List<KeyAndValue> getIpFraudForOption(IpFraudRequestDTO request) {
    //request.setOperationBy(SecurityUtils.checkAdmin());
    return ipFraudMapper.getIpFraudForOption(request);
  }

  @Override
  public void addIpFraud(IpFraudRequestDTO request) {
    if (StringUtils.isEmpty(request.getVersionCode())) {
      throw new ServiceException("版本编号不能为空");
    }
    if (questionInfoService.checkVersionCode(QuestionInfoTypeEnum.IP_FRAUD, request.getIpFraudPlatformCode(), request.getVersionCode())) {
      throw new ServiceException("关联的上层信息已更新,重新获取数据后再提交");
    }
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    request.setCode(UUID.randomUUID().toString().replace("-", ""));
    ipFraudMapper.addIpFraud(request);
    IpFraudPlatformInfo ipFraudPlatform = ipFraudPlatformService.getIpFraudPlatFormByCode(request.getIpFraudPlatformCode());
    redisService.setCacheObject(RedisKeyConstants.IP_FRAUD_INFO.formatKey(request.getCode()), new IpFraudInfo(request.getId(),request.getCode(), request.getIpFraudName(),
        request.getIpFraudPlatformCode(), request.getConfig(), request.getDescription(),ipFraudPlatform.getIpFraudPlatformName()));
  }

  @Override
  public void modifyIpFraud(IpFraudRequestDTO request) {
    if (StringUtils.isEmpty(request.getVersionCode())) {
      throw new ServiceException("版本编号不能为空");
    }
    if (questionInfoService.checkVersionCode(QuestionInfoTypeEnum.IP_FRAUD, request.getIpFraudPlatformCode(), request.getVersionCode())) {
      throw new ServiceException("关联的上层信息已更新,重新获取数据后再提交");
    }
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    Integer integer = ipFraudMapper.modifyIpFraud(request);
    if(integer == 0) {
      throw new ServiceException("code不存在");
    }
    IpFraudInfo ipFraud = ipFraudMapper.getIpFraudByCode(request);
    IpFraudPlatformInfo  ipFraudPlatformInfo = ipFraudPlatformService.getIpFraudPlatFormByCode(request.getIpFraudPlatformCode());
    redisService.setCacheObject(RedisKeyConstants.IP_FRAUD_INFO.formatKey(request.getCode()), new IpFraudInfo(ipFraud.getId(),request.getCode(), request.getIpFraudName(),
            request.getIpFraudPlatformCode(), request.getConfig(), request.getDescription(),ipFraudPlatformInfo.getIpFraudPlatformName()));
    //查找是否存在已关联的修改过的上层数据
    List<QuestionShowInfo> questions = questionInfoService.getQuestionInfosByTypeAndCode(QuestionInfoTypeEnum.IP_FRAUD.name(), request.getCode());
    if(!Collections.isEmpty(questions)){
      questions.forEach(f ->{
        String key = RedisKeyConstants.QUESTION_FIX_INFO.formatKey(request.getCode(), f.getCode());
        if(!redisService.hasKey(key)){
          redisService.setCacheObject(key,true);
        }
      });
    }
//    questionInfoService.fixQuestion(new QuestionInfoRequestDTO(QuestionInfoTypeEnum.IP_FRAUD, request.getCode(), request.getOperationBy()));
  }

  @Override
  public void deleteIpFraud(String ipFraudCode, Long userId) {
    Integer integer = ipFraudMapper.deleteIpFraud(ipFraudCode, userId);
    if(integer == 0) {
      throw new ServiceException("code不存在");
    }
    //删除关联数据
    //删除和任务关联的数据
    ipFraudMapper.deleteMissionIpFraudRelation(ipFraudCode);
    redisService.deleteObject(RedisKeyConstants.IP_FRAUD_INFO.formatKey(ipFraudCode));
  }

  @Override
  @DataScope(deptAlias = "dept",userAlias = "su")
  public IpFraudIDetailVo getIpFraudDetail(String ipFraud) {
    IpFraudRequestDTO ipFraudRequestDTO = new IpFraudRequestDTO();
    ipFraudRequestDTO.setCode(ipFraud);
    return ipFraudMapper.getIpFraudDetail(ipFraudRequestDTO);
  }
}
